
package com.example.cardgame

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.AsyncListUtil
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.io.Serializable

interface DataCallback {
    fun onDataReceived(data: MyData) // 根据你的需求定义参数类型和数量
}
interface DataCallback2 {
    fun onDataReceived2(data: MyData) // 根据你的需求定义参数类型和数量
}
class MainActivity:AppCompatActivity() , DataCallback,DataCallback2 {
    val mainActivity=frameLayout.newInstance()
    val gameActivity=GameActivity.newInstance()
    val gameFrame=GameFrame.newInstance()
    val bundle=Bundle()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.frangment)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        if(savedInstanceState==null){
            supportFragmentManager.beginTransaction()
                .replace(R.id.frame,mainActivity)
                .commit()
        }

        val bottomNV =findViewById<BottomNavigationView>(R.id.bottomNV)
        bottomNV.setOnNavigationItemSelectedListener {
            when(it.itemId){
                R.id.top->{
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.frame,mainActivity)
                        .commit()
                }
                R.id.two->{
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.frame,gameFrame)
                        .addToBackStack(null)
                        .commit()
                }
                R.id.third->{
                    supportFragmentManager.beginTransaction()
                        .replace(R.id.frame,gameActivity)
                        .addToBackStack(null)
                        .commit()
                }
            }
            true
        }

    }
    override fun onDataReceived(data:MyData ) {
        // 处理从Fragment回传的数据
        Log.i("MainActivity","$data")
        bundle.putSerializable("score",data.score)
        mainActivity.arguments=bundle
    }
    override fun onDataReceived2(data: MyData){
        bundle.putSerializable("score2",data.score)
    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.carte,menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId){
            R.id.next->{
                AlertDialog.Builder(this).apply {
                    setTitle("提示")//表示
                    setMessage("是否跳转到编辑页面")//内容
                    setCancelable(false)//是否使用Back关闭对话框
                    setPositiveButton("yes"){//确认按钮点击事件
                            dialog, which ->
                    }
                    setNegativeButton("no"){ //设置取消按钮
                            dialog, which ->
                    }
                    show()
                }
            }
            R.id.connect->{
                AlertDialog.Builder(this).apply {
                    setTitle("提示")//表示
                    setMessage("将链接发送他人？")//内容
                    setCancelable(false)//是否使用Back关闭对话框
                    setPositiveButton("yes"){//确认按钮点击事件
                            dialog, which ->
                    }
                    setNegativeButton("no"){ //设置取消按钮
                            dialog, which ->
                    }
                    show()
                }
            }
        }
        return true
    }
}